package utils

import (
	"encoding/json"
	"fmt"
	"go_async_task/utils/httplib"
	"strconv"
)

func HandleRequestIndex(name string) error {
	// TODO 这里的URL就写死了
	URL := fmt.Sprintf("http://127.0.0.1:9000/index")
	parse := httplib.Get(URL)
	params := map[string]string{
		"name": name,
	}
	parse.Params(params)
	resp, errResp := parse.String()
	if errResp != nil {
		return nil
	}
	fmt.Println("resp:>>> ", resp)
	return nil
}

func AnyToInt(value interface{}) int {
	if value == nil {
		return 0
	}
	switch val := value.(type) {
	case int:
		return int(val)
	case int8:
		return int(val)
	case int16:
		return int(val)
	case int32:
		return int(val)
	case int64:
		return int(val)
	case uint:
		return int(val)
	case uint8:
		return int(val)
	case uint16:
		return int(val)
	case uint32:
		return int(val)
	case uint64:
		return int(val)
	case *string:
		v, err := strconv.Atoi(*val)
		if err != nil {
			return 0
		}
		return v
	case string:
		v, err := strconv.Atoi(val)
		if err != nil {
			return 0
		}
		return v
	case float32:
		return int(val)
	case float64:
		return int(val)
	case bool:
		if val {
			return 1
		} else {
			return 0
		}
	case json.Number:
		v, _ := val.Int64()
		return int(v)
	}
	return 0
}

func AnyToString(value interface{}) string {
	if value == nil {
		return ""
	}
	switch val := value.(type) {
	case *string:
		if val == nil {
			return ""
		}
		return *val
	case string:
		return val
	case int:
		return strconv.Itoa(val)
	case error:
		return val.Error()
	default:
		return fmt.Sprint(value)
	}
}
